<template>
	<view class="home">
		<navbar :isSearch="true" @inputSearch="change" v-model="value"></navbar>
		<view class="home-list">
			<view class="label-box" v-if="is_history">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<text class="label-clear" @click="clear">清空</text>
				</view>
				<view class="label-content" v-if="historyLists.length>0">
					<view class="label-content_item" v-for="(item,index) in historyLists" :key="index" @click="openHistory(item)">
						{{item.name}} 内容
					</view>
				</view>
				<view class="no-data" v-else>
					没有搜索历史
				</view>
			</view>
			<!-- 搜索结果 -->
			<listScroll v-else class="listScroll">
				<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
				<view v-if="searchList.length>0">
					<!-- 卡片 -->
					<listCard  v-for="(item,index) in searchList" :key="item._id" :item="item" @clickCard="setHistory"></listCard>
				</view>
				<view v-if="searchList.length===0 && !loading" class="no-data">
					没有搜索到相关数据
				</view>
			</listScroll>
			<!-- <button type="default" @click="testBtn">添加历史记录</button> -->
		</view>
	</view>
</template>

<script>
	import navbar from "@/components/navbar/navbar.vue";
	import {
		mapState
	} from 'vuex';

	export default {
		name: 'home-search',
		components: {
			navbar,

		},
		data() {
			return {
				is_history:true, // 是否有搜索
				searchList:[],	// 符合搜索的数据
				mark: false,	// 搜索节流
				value:'',		//搜索内容
				loading: false
			}
		},
		computed:{
			...mapState(['historyLists']),	// 搜索历史的数据
		},
		onLoad() {
			
		},
		methods: {
			change(value) {
				console.log('home-search 接受的value', value);
				this.value = value;
				if(!value) {
					clearTimeout(this.timer);
					this.mark = false;
					this.getSearch(value);
					return;
				}
				// 做标记
				if(!this.mark){
					this.mark = true;
					this.timer = setTimeout(()=>{
						this.mark = false;
						this.getSearch(value);
					},1000);
					
				}
			},
			// 获取-文章列表数据
			getSearch(value) {
				if(!value){
					this.searchList = [];
					this.is_history = true;
					return;
				}
				this.is_history = false;
				this.loading = true;
				this.$api.get_search({
						value
					})
					.then(res => {
						const {
							data
						} = res;
						console.log('获取-搜索列表数据=>', res);
						this.loading = false;
						this.searchList = data;
					}).catch(()=>{
						this.loading = false;
					})
			},
			// 存储搜索历史记录
			setHistory(){
				// vuex
				this.$store.dispatch('set_history',{
					name: this.value
				});
			},
			// 打开搜索历史记录
			openHistory(item){
				this.value = item.name;
				this.getSearch(this.value);
			},
			// 清空
			clear(){
				this.$store.dispatch('clear_history');
				uni.showToast({
					title:'清空已完成'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
		background-color: #f5f5f5;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		// border: 1px red solid;

		.label-box {
			background-color: #fff;
			margin-bottom: 10px;

			.label-header {
				display: flex;
				justify-content: space-between;
				font-size: 14px;
				color: #666;
				padding: 10px 15px;
				border-bottom: 1px #f5f5f5 solid;

				.label-title {
					color: $mk-base-color;
				}

				.label-clear {
					color: #30b33a;
					font-weight: bold;
				}
			}

			.label-content {
				display: flex;
				flex-wrap: wrap;
				padding: 15px;
				padding-top: 0;

				.label-content_item {
					padding: 2px 10px;
					margin-top: 12px;
					margin-right: 10px;
					border-radius: 5px;
					border: 1px #666 solid;
					font-size: 14px;
					color: #666;
				}
			}
		}
	}

	.no-data {
		height: 200px;
		line-height: 200px;
		width: 100%;
		text-align: center;
		color: #666;
		font-size: 12px;
	}
</style>
